package com.suncnpap.statistics.controller;

import com.suncnpap.reader.model.Grade;
import com.suncnpap.reader.model.Reader;
import com.suncnpap.reader.service.ClassService;
import com.suncnpap.reader.service.GradeService;
import com.suncnpap.reader.service.ReaderService;
import com.suncnpap.statistics.service.OverPaidService;
import com.suncnpap.statistics.service.WorkLogService;
import com.suncnpap.system.model.School;
import com.suncnpap.system.service.SchoolService;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.app.util.common.HandlerResult;
import com.app.util.common.ObjUtil;
import com.app.util.common.ResponseUtil;
import com.app.util.common.UuidUtil;
import com.app.util.database.mybatis.Page;
import com.app.util.database.mybatis.QueryType;
import com.suncnpap.base.common.GlobaUser;
import com.suncnpap.base.controller.BaseController;
import com.suncnpap.datamanage.common.ExportExcelUtils;
import com.suncnpap.journal.model.BorrowJournalInfo;
import com.suncnpap.journal.model.JournalRegistration;
import com.suncnpap.journal.service.BorrowJournalRecordService;
import com.suncnpap.journal.service.BorrowJournalService;
import com.suncnpap.library.model.BorrowBookInfo;
import com.suncnpap.library.model.CollectionType;
import com.suncnpap.library.model.Library;
import com.suncnpap.library.service.BorrowBookRecordService;
import com.suncnpap.library.service.BorrowBookService;
import com.suncnpap.library.service.CollectionTypeService;
import com.suncnpap.library.service.LibraryService;

/** 
 * 说明：统计Controller
 */
@Controller
@RequestMapping(value="/statistics")
public class StatisticsController extends BaseController {
	
	@Autowired
	ReaderService readerService;
	
	@Autowired
	BorrowBookRecordService borrowBookRecordService;
	
	@Autowired
	LibraryService libraryService;
	
	@Autowired
	BorrowBookService borrowBookService;
	
	@Autowired
	CollectionTypeService collectionTypeService;
	
	@Autowired
	OverPaidService overPaidService;
	
	@Autowired
	BorrowJournalRecordService borrowJournalRecordService;
	
	@Autowired
	BorrowJournalService borrowJournalService;
	
	@Autowired
	WorkLogService workLogService;
	
	@Autowired
	ClassService classService;
	
	@Autowired
	GradeService gradeService;
	
	@Autowired
	SchoolService schoolService;
	
	@RequestMapping(value="/readerBorrowList")
	public void readerBorrowList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		
		if (result.getCode()==0) {

			Page page = this.setPage();
			
			page.put("isUnit", "true");
			
			page.removeQueryAnd("strSchoolId");
			page.addQueryAnd("br.strSchoolId", GlobaUser.getStrSchoolId(request.getHeader("token")));
			
			String strUnitId = this.getParameter("strUnitId", "");
			if(!strUnitId.equals("")){
				page.addQueryAnd("r.strUnitId", QueryType.markGTEQ, strUnitId);
			}
			
			String dtStartCreatDate = this.getParameter("dtStartCreatDate", "");
			if(!dtStartCreatDate.equals("")){
				page.addQueryAnd("br.dtCreatDate", QueryType.markGTEQ, dtStartCreatDate);
			}
			
			String dtEndCreatDate = this.getParameter("dtEndCreatDate", "");
			if(!dtEndCreatDate.equals("")){
				page.addQueryAnd("br.dtCreatDate", QueryType.markLTEQ, dtEndCreatDate);
			}
			
			String strCollectionLocation = this.getParameter("strCollectionLocation", "");
			if(!strCollectionLocation.equals("")){
				page.addQueryAnd("l.strCollectionLocationId", strCollectionLocation);
			}
			
			page = borrowBookRecordService.readerBorrowList(page);
			
			String isExp = this.getParameter("isExp", "");
			if(isExp.equals("1")){
				List<BorrowBookInfo> libraryList = page.getDataList();
				String header = "借阅次数,姓名,读者卡号,部门";
				
				String tableStr = "strCount,strReaderName,strReaderCardNumber,strUnitName";
				
				ExportExcelUtils util = new ExportExcelUtils();
				String path = "upload/exp/";
				String fileName = UuidUtil.get32UUID()+".xls";
				util.exportExcel("读者借阅排行", ObjUtil.strToList(header), ObjUtil.strToList(tableStr),libraryList, path, fileName);
				
				result.put("path", path);
				result.put("fileName", fileName);
			}else{
				result = this.returnPage(result, page);
			}
		}
		
		ResponseUtil.writeJson(result, response);

	}
	
	@RequestMapping(value="/libraryBorrowList")
	public void libraryBorrowList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		
		if (result.getCode()==0) {

			Page page = this.setPage();
			
			page.removeQueryAnd("strSchoolId");
			page.addQueryAnd("br.strSchoolId", GlobaUser.getStrSchoolId(request.getHeader("token")));
			
			String dtStartCreatDate = this.getParameter("dtStartCreatDate", "");
			if(!dtStartCreatDate.equals("")){
				page.addQueryAnd("br.dtCreatDate", QueryType.markGTEQ, dtStartCreatDate);
			}
			
			String dtEndCreatDate = this.getParameter("dtEndCreatDate", "");
			if(!dtEndCreatDate.equals("")){
				page.addQueryAnd("br.dtCreatDate", QueryType.markLTEQ, dtEndCreatDate);
			}
			
			String strCollectionLocation = this.getParameter("strCollectionLocation", "");
			if(!strCollectionLocation.equals("")){
				page.addQueryAnd("l.strCollectionLocationId", strCollectionLocation);
			}
			
			page = borrowBookRecordService.libraryBorrowList(page);
			
			String isExp = this.getParameter("isExp", "");
			if(isExp.equals("1")){
				List<BorrowBookInfo> list = page.getDataList();
				String header = "借阅次数,正题名,作者,图书条码,ISBN,每套价格,书屋位置";

				String tableStr = "strCount,strPositiveTitle,strFirstResponsible,strBookBarcode,strISBN,floatPricePer,strCollectionLocation";
				
				ExportExcelUtils util = new ExportExcelUtils();
				String path = "upload/exp/";
				String fileName = UuidUtil.get32UUID()+".xls";
				util.exportExcel("图书借阅排行", ObjUtil.strToList(header), ObjUtil.strToList(tableStr),list, path, fileName);
				
				result.put("path", path);
				result.put("fileName", fileName);
			}else{
				result = this.returnPage(result, page);
			}
		}
		
		ResponseUtil.writeJson(result, response);

	}
	
	@RequestMapping(value="/librarylist")
	public void librarylist(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		
		if (result.getCode()==0) {

			Page page = this.setPage();
			
			String dtStartCreatDate = this.getParameter("dtStartCreatDate", "");
			if(!dtStartCreatDate.equals("")){
				page.addQueryAnd("dtCreatDate", QueryType.markGTEQ, dtStartCreatDate);
			}
			
			String dtEndCreatDate = this.getParameter("dtEndCreatDate", "");
			if(!dtEndCreatDate.equals("")){
				page.addQueryAnd("dtCreatDate", QueryType.markLTEQ, dtEndCreatDate);
			}
			
			String strCollectionLocation = this.getParameter("strCollectionLocation", "");
			if(!strCollectionLocation.equals("")){
				page.addQueryAnd("strCollectionLocationId", strCollectionLocation);
			}
			
			page = libraryService.libraryList(page);
			String isExp = this.getParameter("isExp", "");
			if(isExp.equals("1")){
				List<Library> list = page.getDataList();
				String header = "正题名,ISBN,第一责任者,出版社,分类号,单价,订购套数,总计";

				String tableStr = "strPositiveTitle,strISBN,strFirstResponsible,strPublisher,strClassNumber,floatPricePer,strBookNumber,fTotalPrices";
				
				ExportExcelUtils util = new ExportExcelUtils();
				String path = "upload/exp/";
				String fileName = UuidUtil.get32UUID()+".xls";
				util.exportExcel("书屋图书信息统计", ObjUtil.strToList(header), ObjUtil.strToList(tableStr),list, path, fileName);
				
				result.put("path", path);
				result.put("fileName", fileName);
			}else{
				result = this.returnPage(result, page);
			}
		}
		
		ResponseUtil.writeJson(result, response);

	}
	
	
	@RequestMapping(value="/overList")
	public void overList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		
		if (result.getCode()==0) {

			Page page = this.setPage();
			
			page.put("user", "true");
			page.put("isUnit", "true");
			page.addQueryAnd("r.strSchoolId", result.get("strSchoolId"));
			page.addQueryAnd("l.strSchoolId", result.get("strSchoolId"));
			
			String strUnitId = this.getParameter("strUnitId","");
			if(!strUnitId.equals("")){
				page.put("strUnitId", strUnitId);
			}
			
			String dtStartCreatDate = this.getParameter("dtStartCreatDate", "");
			if(!dtStartCreatDate.equals("")){
				page.addQueryAnd("br.dtCreatDate", QueryType.markGTEQ, dtStartCreatDate);
			}
			
			String dtEndCreatDate = this.getParameter("dtEndCreatDate", "");
			if(!dtEndCreatDate.equals("")){
				page.addQueryAnd("br.dtCreatDate", QueryType.markLTEQ, dtEndCreatDate);
			}
			
			String keyName = this.getParameter("keyName", "");
			String keywords = this.getParameter("keywords", "");
			if(!keyName.equals("")){
				if(keyName.equals("strBookBarcode")){
					page.addQueryAnd("br.strBookBarcode", keywords);
				}else if(keyName.equals("strPositiveTitle")){
					page.addQueryAnd("l.strPositiveTitle", keywords);
				}else if(keyName.equals("strName")){
					page.addQueryAnd("r.strName", keywords);
				}else if(keyName.equals("strCardNumber")){
					page.addQueryAnd("br.strReaderCardNumber", keywords);
				}
			}
			
			page.put("isOver", "true");
			page.put("over", "true");
			
			page.removeQueryAnd("strSchoolId");
			page.addQueryAnd("br.strSchoolId", GlobaUser.getStrSchoolId(request.getHeader("token")));
			
			page = borrowBookService.listInfo(page);
			
			String isExp = this.getParameter("isExp", "");
			if(isExp.equals("1")){
				List<BorrowBookInfo> list = page.getDataList();
				String header = "卡号,姓名,部门,所借图书,图书条码,借阅时间,应还时间,超期天数,操作员";

				String tableStr = "strReaderCardNumber,strReaderName,strUnitName,strPositiveTitle,strBookBarcode,dtBorrowDate,dtReturnDate,strDays,strCreatName";
				
				ExportExcelUtils util = new ExportExcelUtils();
				String path = "upload/exp/";
				String fileName = UuidUtil.get32UUID()+".xls";
				util.exportExcel("到期图书统计", ObjUtil.strToList(header), ObjUtil.strToList(tableStr),list, path, fileName);
				
				result.put("path", path);
				result.put("fileName", fileName);
			}else{
				result = this.returnPage(result, page);
			}
		}
		
		ResponseUtil.writeJson(result, response);

	}
	
	@RequestMapping(value="/categoryList")
	public void categorylist(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		
		if (result.getCode()==0) {

			Page page = this.setPage();
			
			String dtStartCreatDate = this.getParameter("dtStartCreatDate", "");
			if(!dtStartCreatDate.equals("")){
				page.addQueryAnd("dtCreatDate", QueryType.markGTEQ, dtStartCreatDate);
			}
			
			String dtEndCreatDate = this.getParameter("dtEndCreatDate", "");
			if(!dtEndCreatDate.equals("")){
				page.addQueryAnd("dtCreatDate", QueryType.markLTEQ, dtEndCreatDate);
			}
			
			String strClassNumber = this.getParameter("strClassNumber", "");
			if(!strClassNumber.equals("")){
				page.addQueryAnd("strClassNumber", strClassNumber);
			}
			
			String strType = this.getParameter("strType", "");
			if(!strType.equals("")){
				
				if(strType.equals("all")){
					
					page.removeQueryAnd("strSchoolId");
					page.addQueryAnd("l.strSchoolId", GlobaUser.getStrSchoolId(request.getHeader("token")));
					
					if(!dtStartCreatDate.equals("")){
						page.removeQueryAnd("dtCreatDate");
						page.addQueryAnd("l.dtCreatDate", QueryType.markGTEQ, dtStartCreatDate);
					}
					
					if(!dtEndCreatDate.equals("")){
						page.removeQueryAnd("dtCreatDate");
						page.addQueryAnd("l.dtCreatDate", QueryType.markLTEQ, dtEndCreatDate);
					}
					
					if(!strClassNumber.equals("")){
						page.removeQueryAnd("strClassNumber");
						page.addQueryAnd("l.strClassNumber", strClassNumber);
					}
					page = libraryService.collectionTypelist(page);
				}else{
					CollectionType type = collectionTypeService.findById(strType);
					if(type!=null){
						String collections = type.getStrCollectionLocationId();
						
						if(!collections.equals("")){
							page.addQueryAnd("strCollectionLocationId", QueryType.MarkINREVERSE, ObjUtil.strToSql(collections));
						}
					}
					page = libraryService.categoryList(page);
				}
			}else{
				String strCollectionLocation = this.getParameter("strCollectionLocation", "");
				if(!strCollectionLocation.equals("")){
					page.addQueryAnd("strCollectionLocationId", strCollectionLocation);
				}
				page = libraryService.categoryList(page);
			}
			
			String isExp = this.getParameter("isExp", "");
			if(isExp.equals("1")){
				List<Library> list = page.getDataList();
				
				String header = "";
				String tableStr = "";
				
				if(strType.equals("all")){
					header = "乡镇,种数,册数,金额,丢失,馆藏,借出";
					tableStr = "strName,intTotal,strRoyalty,fTotalPrices,intLossCount,intTotaIinventory,intBorrowCount";
				}else{
					header = "学科类目,种数,册数,金额";
					tableStr = "strCategoryName,intTotal,strRoyalty,fTotalPrices";
				}
				
				ExportExcelUtils util = new ExportExcelUtils();
				String path = "upload/exp/";
				String fileName = UuidUtil.get32UUID()+".xls";
				util.exportExcel("馆藏分类", ObjUtil.strToList(header), ObjUtil.strToList(tableStr),list, path, fileName);
				
				result.put("path", path);
				result.put("fileName", fileName);
			}else{
				result = this.returnPage(result, page);
			}
		}
		
		ResponseUtil.writeJson(result, response);

	}
	
	@RequestMapping(value="/overPaidList")
	public void overPaidList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		
		if (result.getCode()==0) {

			Page page = this.setPage();
			
			String strUnitId = this.getParameter("strUnitId","");
			if(!strUnitId.equals("")){
				page.put("strUnitId", strUnitId);
			}
			
			String dtStartCreatDate = this.getParameter("dtStartCreatDate", "");
			if(!dtStartCreatDate.equals("")){
				page.addQueryAnd("br.dtCreatDate", QueryType.markGTEQ, dtStartCreatDate);
			}
			
			String dtEndCreatDate = this.getParameter("dtEndCreatDate", "");
			if(!dtEndCreatDate.equals("")){
				page.addQueryAnd("br.dtCreatDate", QueryType.markLTEQ, dtEndCreatDate);
			}
			
			String keyName = this.getParameter("keyName", "");
			String keywords = this.getParameter("keywords", "");
			if(!keyName.equals("")){
				if(keyName.equals("strBookBarcode")){
					page.addQueryAnd("br.strBookBarcode", keywords);
				}else if(keyName.equals("strPositiveTitle")){
					page.addQueryAnd("l.strPositiveTitle", keywords);
				}else if(keyName.equals("strName")){
					page.addQueryAnd("r.strName", keywords);
				}else if(keyName.equals("strCardNumber")){
					page.addQueryAnd("br.strCardNumber", keywords);
				}
			}
			
			page.removeQueryAnd("strSchoolId");
			page.addQueryAnd("br.strSchoolId", GlobaUser.getStrSchoolId(request.getHeader("token")));
			
			page.addQueryAnd("r.strSchoolId", result.get("strSchoolId"));
			page.addQueryAnd("l.strSchoolId", result.get("strSchoolId"));
			
			page = overPaidService.listInfo(page);
			
			String isExp = this.getParameter("isExp", "");
			if(isExp.equals("1")){
				List<BorrowBookInfo> list = page.getDataList();
				String header = "书名,图书条码,读者卡号,读者名称,部门,赔偿金额,实收金额,罚款时间,操作员";

				String tableStr = "strPositiveTitle,strBookBarcode,strReaderCardNumber,strReaderName,strUnitName,strAmountPaid,strAmountReceipt,dtCreatDate,strCreatName";
				
				ExportExcelUtils util = new ExportExcelUtils();
				String path = "upload/exp/";
				String fileName = UuidUtil.get32UUID()+".xls";
				util.exportExcel("超期罚款", ObjUtil.strToList(header), ObjUtil.strToList(tableStr),list, path, fileName);
				
				result.put("path", path);
				result.put("fileName", fileName);
			}else{
				result = this.returnPage(result, page);
			}
		}
		
		ResponseUtil.writeJson(result, response);

	}
	
	/**
	 * 去往列表页面
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/journalList")
	public void journalList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		
		if (result.getCode()==0) {

			Page page = this.setPage();
			
			page.put("user", "true");
			page.addQueryAnd("r.strSchoolId", result.get("strSchoolId"));
			page.addQueryAnd("l.strSchoolId", result.get("strSchoolId"));
			
			page.removeQueryAnd("strSchoolId");
			page.addQueryAnd("br.strSchoolId", GlobaUser.getStrSchoolId(request.getHeader("token")));
			
			String strUnitId = this.getParameter("strUnitId","");
			if(!strUnitId.equals("")){
				page.put("strUnitId", strUnitId);
				page.addQueryAnd("r.strUnitId", strUnitId);
			}
			
			String dtStartCreatDate = this.getParameter("dtStartCreatDate", "");
			if(!dtStartCreatDate.equals("")){
				page.addQueryAnd("br.dtCreatDate", QueryType.markGTEQ, dtStartCreatDate);
			}
			
			String dtEndCreatDate = this.getParameter("dtEndCreatDate", "");
			if(!dtEndCreatDate.equals("")){
				page.addQueryAnd("br.dtCreatDate", QueryType.markLTEQ, dtEndCreatDate);
			}
			
			String keyName = this.getParameter("keyName", "");
			String keywords = this.getParameter("keywords", "");
			if(!keyName.equals("")){
				if(keyName.equals("strJournalBarcode")){
					page.addQueryAnd("br.strJournalBarcode", keywords);
				}else if(keyName.equals("strJournalTitle")){
					page.addQueryAnd("l.strJournalTitle", keywords);
				}else if(keyName.equals("strUnifiedIssue")){
					page.addQueryAnd("l.strUnifiedIssue", keywords);
				}else if(keyName.equals("strName")){
					page.addQueryAnd("r.strName", keywords);
				}else if(keyName.equals("strCardNumber")){
					page.addQueryAnd("br.strReaderCardNumber", keywords);
				}
			}
			
			
			String strType = this.getParameter("strType","");
			if(!strType.equals("")){
				if(strType.equals("未还")){
					page = borrowJournalService.listInfo(page);
				}else if(strType.equals("已还")){
					page.addQueryAnd("br.isReturn",1);
					page = borrowJournalRecordService.listInfo(page);
				}
			}else{
				page = borrowJournalRecordService.listInfo(page);
			}
			
			String isExp = this.getParameter("isExp", "");
			if(isExp.equals("1")){
				List<BorrowJournalInfo> libraryList = page.getDataList();
				String header = "卡号,姓名,部门,期刊名,期号,邮发代号,统一刊号,ISSN,期刊条码,借阅时间,应还时间,操作员";
				
				String tableStr = "strReaderCardNumber,strReaderName,strUnitName,strJournalTitle,strRegistrationIssue,strPostcode,strUnifiedIssue,strIssue,strJournalBarcode,dtBorrowDate,dtReturnDate,strCreatName";
				
				ExportExcelUtils util = new ExportExcelUtils();
				String path = "upload/exp/";
				String fileName = UuidUtil.get32UUID()+".xls";
				util.exportExcel("期刊借阅统计", ObjUtil.strToList(header), ObjUtil.strToList(tableStr),libraryList, path, fileName);
				
				result.put("path", path);
				result.put("fileName", fileName);
			}else{
				result = this.returnPage(result, page);
			}
		}
		
		ResponseUtil.writeJson(result, response);

	}
	
	/**
	 * 去往列表页面
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/workLogList")
	public void workLogList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		
		if (result.getCode()==0) {

			Page page = this.setPage();
			
			page.removeQueryAnd("strSchoolId");
			page.addQueryAnd("w.strSchoolId", GlobaUser.getStrSchoolId(request.getHeader("token")));
			
			String dtStartCreatDate = this.getParameter("dtStartCreatDate", "");
			if(!dtStartCreatDate.equals("")){
				page.addQueryAnd("w.dtCreatDate", QueryType.markGTEQ, dtStartCreatDate);
			}
			
			String dtEndCreatDate = this.getParameter("dtEndCreatDate", "");
			if(!dtEndCreatDate.equals("")){
				page.addQueryAnd("w.dtCreatDate", QueryType.markLTEQ, dtEndCreatDate);
			}
			
			String strCollectionLocation = this.getParameter("strCollectionLocation", "");
			if(!strCollectionLocation.equals("")){
				page.addQueryAnd("a.strCollectionLocationId", strCollectionLocation);
			}
			
			page = workLogService.workLogList(page);
			result = this.returnPage(result, page);
		}
		
		ResponseUtil.writeJson(result, response);

	}
	
	/**
	 * 列表页面数据处理，包括查询
	 * @param page
	 * @param response
	 */
	@RequestMapping(value="/integralList")
	public void integralList(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		
		if (result.getCode()==0) {

			Page page = this.setPage();
			
			String strGradeId = this.getParameter("strGradeId", "");
			if(!strGradeId.equals("")){
				page.addQueryAnd("strGradeId", strGradeId);
			}
			
			String strClassId = this.getParameter("strClassId", "");
			if(!strClassId.equals("")){
				page.addQueryAnd("strClassId", strClassId);
			}

			String strCardNumber = this.getParameter("strCardNumber", "");
			if(!strCardNumber.equals("")){
				page.addQueryAnd("strCardNumber", strCardNumber);
			}
			page = readerService.list(page);
			
			String isExp = this.getParameter("isExp", "");
			if(isExp.equals("1")){
				List<Reader> list = page.getDataList();
				String header = "姓名,积分";

				String tableStr = "strName,intIntegral";
				
				ExportExcelUtils util = new ExportExcelUtils();
				String path = "upload/exp/";
				String fileName = UuidUtil.get32UUID()+".xls";
				util.exportExcel("积分统计", ObjUtil.strToList(header), ObjUtil.strToList(tableStr),list, path, fileName);
				
				result.put("path", path);
				result.put("fileName", fileName);
			}else{
				result = this.returnPage(result, page);
			}
		}
		
		ResponseUtil.writeJson(result, response);

	}
	
	/**
	 * 大数据看板数据处理
	 * @param page
	 * @param response
	 */
	@RequestMapping(value="/index")
	public void index(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		
		if (result.getCode()==0) {

			String strSchoolId = GlobaUser.getStrSchoolId(request.getHeader("token"));
			Page page = new Page();
			page.addQueryAnd("strSchoolId", strSchoolId);
			int allCount = libraryService.findCount(page);
			result.put("allCount", allCount);
			
			Page readerBorrowPage = this.setPage();
			readerBorrowPage.setUsePage(false);
			readerBorrowPage.removeQueryAnd("strSchoolId");
			readerBorrowPage.addQueryAnd("r.strSchoolId", strSchoolId);
			readerBorrowPage = borrowBookRecordService.readerBorrowGroupList(readerBorrowPage);
			List<BorrowBookInfo> list = readerBorrowPage.getDataList();
			result.put("borrowList", list);
			
			Map<String, List<BorrowBookInfo>> map = ObjUtil.listToMapByField(list, "strGradeClassName");
			List<Map<String, Object>> objList = new ArrayList<Map<String, Object>>();
	        for (Map.Entry<String, List<BorrowBookInfo>> entry : map.entrySet()) {
	            System.out.println(entry.getKey() + ": " + entry.getValue());
	            Map<String, Object> objMap = new HashMap<String, Object>();
				objMap.put("title", entry.getKey());
				objMap.put("list", entry.getValue());
				objList.add(objMap);
	        }
			
	        result.put("classBorrowList", objList);
	        
	        School school = schoolService.findById(strSchoolId);
	        if(school!=null){
	        	result.put("strPicUrl", school.getStrPicUrl());
	        }
	        
	        Page libraryBorrowPage = this.setPage();
	        libraryBorrowPage.removeQueryAnd("strSchoolId");
	        libraryBorrowPage.addQueryAnd("br.strSchoolId", strSchoolId);
	        libraryBorrowPage = borrowBookRecordService.libraryBorrowList(libraryBorrowPage);
	        List<BorrowBookInfo> libraryBorrowList = libraryBorrowPage.getDataList();
	        result.put("libraryBorrowList", libraryBorrowList);
	        
	        Page countPage = this.setPage();
	        countPage.removeQueryAnd("strSchoolId");
	        countPage.addQueryAnd("br.strSchoolId", strSchoolId);
	        BorrowBookInfo obj = borrowBookRecordService.libraryBorrowStatistics(countPage);
	        
	        DecimalFormat g1=new DecimalFormat("00000");
	        if(obj!=null){
	        	String strCount = obj.getStrCount();
	        	int count = Integer.valueOf(strCount);
	        	result.put("strCount", obj.getStrCount().length()==5?obj.getStrCount():(g1.format(count)+""));
	        	result.put("intDayBorrowCount", obj.getIntDayBorrowCount()+"");
	        	result.put("intDayReturnCount", obj.getIntDayReturnCount()+"");
	        }
	        
	        Page hourBorrowStatisticsPage = this.setPage();
	        hourBorrowStatisticsPage.addQueryAnd("strSchoolId", strSchoolId);
	        hourBorrowStatisticsPage = borrowBookRecordService.hourBorrowStatistics(hourBorrowStatisticsPage);
	        List<BorrowBookInfo> hourBorrowList = hourBorrowStatisticsPage.getDataList();
	        result.put("hourBorrowList", hourBorrowList);
	        
	        
		}
		
		ResponseUtil.writeJson(result, response);

	} 
}
